/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doc2db.interfaces.action.InsUpd;

import doc2db.DbManager.DbSelects;
import doc2db.Keey;
import doc2db.UserProp;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Iterator;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;

/**
 *
 * @author yura
 */
class DbClientDialogUpdata extends DbClientDialogInsert{

    public DbClientDialogUpdata(JFrame f, String tableName, JEditorPane _p) {
        super(f, tableName, _p);
        final String tN = "" + tableName;
        for (final TableColumn tableColumn : list) {
            final JCheckBox ch = tableColumn.forUpdate;
            final String s = "" + tableColumn.getColumnName();
            ch.setVisible(true);
            ch.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    if(ch.isSelected()){
                        String [] arr = getDataFromDb(tN, s);
                        if (arr != null) {
                            tableColumn.setForUpdate(arr);
                            repaint();
                        } else{
                            ch.setSelected(false);
                            repaint();
                        }
                    }else{
                        tableColumn.setForInsert();
                    }
                }
            });
        }
    }

    private String [] getDataFromDb(String tableName, String columnName){
        String[] result = null;
        HashSet hs = new HashSet();
        UserProp up = new UserProp();
        String str = "select \""+ columnName +"\" from " + tableName + ";";
        sqlText.setText(sqlText.getText() + "\n" + str);
        try {
            DbSelects query = new DbSelects(up.getURL(), up.getUserProp());
            if (query.sendQuery(str)) {
                int rows = query.getRows();
                for (int i = 1; i <= rows; i++) {
                    hs.add("" + query.getString(i, 1));
                }
            }
        } catch (Exception ex) {
            if (Keey.debug) {
                System.err.println(ex);
            }
        }

        int j = 0;
        result = new String[hs.size()];
        Iterator i = hs.iterator();
            while(i.hasNext())
                result[j++] = "" + i.next();

        return result;
    }
    
    public String getWHERE(){
        StringBuffer s = new StringBuffer();
        boolean b = false;
        s.append("WHERE ");
        for (TableColumn tableColumn : list) {
            if(! tableColumn.dataFromTabelColumn.isVisible())
                continue;

            if(b)
                s.append("AND");
            s.append(" \"");
            s.append(tableColumn.getColumnName());
            s.append("\"=\'");
            s.append(tableColumn.getText());
            s.append("\' ");
            b = true;
        }
        return s.toString();
    }

    public String getSET(){
        StringBuffer s = new StringBuffer();
        boolean b = false;
        s.append("SET ");
        for (TableColumn tableColumn : list) {
            if(tableColumn.dataFromTabelColumn.isVisible()
                    || tableColumn.getText().equals(""))
                continue;
            if(b)
                s.append(",");
            s.append(" \"");
            s.append(tableColumn.getColumnName());
            s.append("\"=\'");
            s.append(tableColumn.getText());
            s.append("\' ");
            b = true;
        }
        return s.toString();
    }
    
}
